package com.umeox.watch.moto.chat.service

import android.app.IntentService
import android.content.Intent
import com.umeox.moto.common.log.Logger
import com.umeox.watch.moto.chat.db.AppDatabase
import com.umeox.watch.moto.chat.db.ContactInfo
import com.umeox.watch.moto.chat.db.DBUtils
import com.umeox.watch.moto.dataservice.DataProvider

/**
 * Created by Rambo 2021/8/18
 * Describe:拉起进程,初始化数据库
 */
class DataBaseService : IntentService("DataBaseService"){


    override fun onHandleIntent(intent: Intent?) {
        if(intent == null){
            return
        }

        //所有的列表数据（好友+群聊）
        val contactList: MutableList<ContactInfo> = DBUtils.getContactList() as MutableList<ContactInfo>
        val buddies: List<ContactInfo> = DBUtils.getBuddiesFromProvider()
        val imGroups: List<ContactInfo> = DBUtils.getIMGroupFromProvider()
        Logger.e("DataBaseService", "本地联系人(好友+群聊）列表：$contactList")
        Logger.e("DataBaseService", "Provider好友列表：$buddies")
        Logger.e("DataBaseService", "Provider群聊列表：$imGroups")
        contactList.removeAll(buddies)
        contactList.removeAll(imGroups)
        Logger.e("DataBaseService", "需要移除的联系人列表：$contactList")
        if (contactList.isNotEmpty()) {
            for (contactInfo in contactList) {
                AppDatabase.get().chatMessageDao().deleteByFriendId(contactInfo.friendId)
                AppDatabase.get().contactInfoDao().delete(contactInfo)
                DataProvider.ChatContactProvider.notifyChange()
            }
        }
    }

}